Spatial Data

Getting Spatial Data

cds.load <- st_read("./cb_2015_us_cd114_500k/cb_2015_us_cd114_500k.shp")
## Reading layer `cb_2015_us_cd114_500k' from data source 
##   `C:\Users\T112332\OneDrive - Allegheny County\Documents\GitHub\welcome-to-the-tidyverse\08-Maps\cb_2015_us_cd114_500k\cb_2015_us_cd114_500k.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 441 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -179.1489 ymin: -14.5487 xmax: 179.7785 ymax: 71.36516
## Geodetic CRS:  NAD83
plot(cds.load)

polls <- st_read("Allegheny_County_Polling_Place_Locations_November_2016.geojson")
## Reading layer `Allegheny_County_Polling_Place_Locations_November_2016' from data source `C:\Users\T112332\OneDrive - Allegheny County\Documents\GitHub\welcome-to-the-tidyverse\08-Maps\Allegheny_County_Polling_Place_Locations_November_2016.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 1323 features and 33 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -80.29643 ymin: 40.19836 xmax: -79.70033 ymax: 40.67336
## Geodetic CRS:  WGS 84

Basic Plot

plot(cds.load)

Merging Table Data to your shapefile

op_data <- read_excel("Geographies_of_Opportunity_Ranking_Well_Being_by_Congressional_District_(114th_Congress).xlsx") %>%
  mutate(Number = str_pad(Number, 4, pad = "0"))

# Merge with Left Join
cds <- cds.load %>%
  left_join(op_data, by = c("GEOID" = "Number"))

ggplot polygon

ggplot(data=cds, aes(fill = `Graduate Degree (%)`)) + 
  geom_sf() +
  theme_void()

## ggplot points

ggplot(data=polls, aes(color = Region)) + 
  geom_sf() +
  theme_void()

## ggmap

# Get the boundaries of your file to get the basemap
bbox <-st_bbox(polls) %>% setNames(c("left", "bottom", "right", "top"))

# Get the API Key
ggmap::register_stadiamaps(Sys.getenv("GGMAP_STADIAMAPS_API_KEY"))

# Load API Basemap
map <- get_stadiamap(bbox, maptype = "stamen_toner_lite")
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
# Add map info to basemap
map %>% 
    ggmap() +   
    coord_sf(crs = st_crs(3857)) +  
    geom_sf(data=polls, aes(color = Region), inherit.aes = FALSE) +  
    theme_map()
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

Leaflet

Blank map with basemap

leaflet() %>%
  addProviderTiles(providers$Esri.WorldImagery)

Blank map with no wrap

leaflet() %>%
  addProviderTiles(providers$Esri.WorldImagery, options = providerTileOptions(noWrap = TRUE))

Shape

Lines

rivers <- st_read("./ne_10m_rivers_lake_centerlines")
## Reading layer `ne_10m_rivers_lake_centerlines' from data source 
##   `C:\Users\T112332\OneDrive - Allegheny County\Documents\GitHub\welcome-to-the-tidyverse\08-Maps\ne_10m_rivers_lake_centerlines' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1455 features and 34 fields (with 1 geometry empty)
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: -164.9035 ymin: -52.15773 xmax: 177.5204 ymax: 75.79348
## Geodetic CRS:  WGS 84
leaflet(data = rivers) %>%
  addProviderTiles("Esri.WorldTerrain", options = providerTileOptions(noWrap = TRUE)) %>%
  addPolylines(color = "#63CBD3")

Line with Popups

leaflet(data = rivers) %>%
  addProviderTiles("Esri.WorldTerrain", options = providerTileOptions(noWrap = TRUE)) %>%
  addPolylines(color = "#63CBD3", popup = ~name_en)

Points

Circle Markers

potholes <- read.csv("311_potholes.csv")

# Custom Palette
pal311 <- colorFactor(c("#d73027", "#1a9850"), c("Closed", "Open"))

leaflet() %>%
  addProviderTiles("OpenStreetMap.HOT") %>%
  addCircleMarkers(data = potholes, lng = ~X, lat = ~Y, radius = 1.5, color = ~pal311(STATUS)) %>%
  addLegend(position = "topright" , pal = pal311, values = potholes$STATUS, title = "Status")

Points from GEOJSON

leaflet(data = polls) %>%
  addProviderTiles("OpenStreetMap.Mapnik") %>%
  addMarkers(popup = ~paste0(LocName, ": ", NewAddress, " ", City, " PA ", Zip))

Points Awesome Markers

potOver <- read.csv("potholesOvergrowth.csv")

icons <- awesomeIconList(
  Potholes = makeAwesomeIcon(icon = "road", library = "fa", markerColor = "gray"),
  Overgrowth = makeAwesomeIcon(icon = "leaf", library = "fa", markerColor = "green")
)

leaflet(data = potOver) %>%
  addProviderTiles("OpenStreetMap.HOT") %>%
  addAwesomeMarkers(lng = ~X, lat = ~Y, icon = ~icons[REQUEST_TYPE], popup = ~REQUEST_TYPE)